/**
 * @File		: OkBtnMouseListener.java
 * @Subject		: Ok button event handler
 * @author 		: Isaac Parenteau	locus2k@gmail.com
 * @Date		: October 12th, 2011
 * @Licence		: Licensed under the GPU LGPL
 */
package mil.cdc.listeners;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JFrame;

import mil.cdc.main.Main;
import mil.cdc.utils.CDCConstants;
import mil.cdc.windows.EndWindow;
import mil.cdc.windows.ErrorWindow;
import mil.cdc.windows.HowToWindow;
import mil.cdc.windows.MainWindow;
import mil.cdc.windows.QuestionBankWindow;
import mil.cdc.windows.ReviewWindow;
import mil.cdc.windows.PrintWindow;

public class CustomMouseListener implements MouseListener {

	private JFrame currentFrame;
	private int control = -1;
	
	/**
	 * Used for handling various different button clicks
	 * @param currentFrame Current frame the button is attached to
	 * @param control CDCCosntants shows what the control values are
	 */
	public CustomMouseListener(JFrame currentFrame, int control) {
		this.currentFrame = currentFrame;
		this.control = control;
	}
	
	@Override
	public void mouseClicked(MouseEvent event) {
		if (event.getButton() == MouseEvent.BUTTON1) {
			if (currentFrame instanceof QuestionBankWindow) {
				QuestionBankWindow qbw = (QuestionBankWindow)currentFrame;
				if (control == CDCConstants.SUBMIT) {
					qbw.loadQuestions();
				}
				else if (control == CDCConstants.CLOSE) {
					Main.close();
					qbw.dispose();
				}
			}
			else if (currentFrame instanceof MainWindow) {
				MainWindow window = (MainWindow)currentFrame;
				if(control == CDCConstants.CURRENT_QUESTION) {
					window.submitAnswer();
				}
				else if (control == CDCConstants.NEXT_QUESTION) {
					window.getNextQuestion();
				}
				else if (control == CDCConstants.PREV_QUESTION) {
					window.getPrevQuestion();
				}
				else if (control == CDCConstants.CLOSE) {
					window.close();
				}
			}
			else if (currentFrame instanceof EndWindow) {
				EndWindow window = (EndWindow)currentFrame;
				
				if (control == CDCConstants.AGAIN) {
					window.reloadTest();
				}
				
				else if (control == CDCConstants.CLOSE) {
					window.close();
				}
				else if (control == CDCConstants.DISPLAY_QUESTIONS) {
					window.displayMissedQuestions();
				}
				else if (control == CDCConstants.REVIEW_QUESTIONS) {
					window.viewMissedQuestions();
				}
				else if (control == CDCConstants.OPEN_PRINT) {
					window.openPrintOption();
				}
			}
			else if (currentFrame instanceof ErrorWindow) {
				if (control == CDCConstants.CLOSE) {
                     ErrorWindow window = (ErrorWindow)currentFrame;
                     window.reset();
				}
			}
			else if (currentFrame instanceof ReviewWindow) {
				if (control == CDCConstants.CLOSE) {
                     ((ReviewWindow)currentFrame).dispose();
				}
			}
			else if (currentFrame instanceof HowToWindow) {
				if (control == CDCConstants.CLOSE) {
					((HowToWindow)currentFrame).dispose();
				}
			}
			else if (currentFrame instanceof PrintWindow) {
				if (control == CDCConstants.CLOSE) {
					((PrintWindow)currentFrame).dispose();
				}
				else if (control == CDCConstants.PRINT) {
					((PrintWindow)currentFrame).print();
				}
				
			}
			else 
				if (control == CDCConstants.CLOSE) {
					currentFrame.dispose();
				}
		}
	}

	@Override
	public void mouseEntered(MouseEvent event) { }

	@Override
	public void mouseExited(MouseEvent event) { }

	@Override
	public void mousePressed(MouseEvent event) { }

	@Override
	public void mouseReleased(MouseEvent event) { }
}
